﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Festival_Locator.Admin
{
	public partial class ManageRoles : BasePage   //System.Web.UI.Page
	{
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ListBox1.DataSource = Roles.GetAllRoles();
                ListBox1.DataBind();
                ListBox2.DataSource = Membership.GetAllUsers();
                ListBox2.DataBind();
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());
            ListBox3.DataBind();
        }

        protected void Button1_Click(object sender, System.EventArgs e)
        {
            string[] allRoles = Roles.GetRolesForUser(ListBox2.SelectedItem.Text.ToString());
            if (allRoles.Length == 0)
            {
                Roles.AddUserToRole(ListBox2.SelectedItem.Text.ToString(), ListBox1.SelectedItem.Text.ToString());
            }
            else
            {
                Roles.RemoveUserFromRoles(ListBox2.SelectedItem.Text.ToString(), allRoles);
                Roles.AddUserToRole(ListBox2.SelectedItem.Text.ToString(), ListBox1.SelectedItem.Text.ToString());
            }
            ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());
            ListBox3.DataBind();
            Label1.Text = ListBox2.SelectedItem.Text.ToString() +
                " User Added in " +
                ListBox1.SelectedItem.Text.ToString() +
                " Role successfully!";
        } 
	}
}